package com.zide.ams.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zide.ams.domain.po.AmsArticle;
import com.zide.ams.domain.po.AmsCollect;
import com.zide.ams.domain.vo.result.AmsCollectResultVO;
import com.zide.ams.domain.vo.save.AmsCollectSaveVO;
import com.zide.ams.domain.vo.search.AmsCollectSearchVO;
import com.zide.ams.mapper.AmsArticleMapper;
import com.zide.ams.mapper.AmsCollectMapper;
import com.zide.ams.service.IAmsCollectService;
import com.zide.common.constant.HttpStatus;
import com.zide.common.core.domain.PageResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.zide.common.core.vo.CommonParamIdVO;
import com.zide.common.exception.ServiceException;
import com.zide.common.utils.SecurityUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 文章收藏Service业务层处理
 * 
 * @author zide
 * @date 2024-04-29
 */
@Slf4j
@Service
public class AmsCollectServiceImpl implements IAmsCollectService {
    @Resource
    private AmsCollectMapper amsCollectMapper;
    @Resource
    private AmsArticleMapper amsArticleMapper;

    /**
     * 查询文章收藏列表
     */
    @Override
    public PageResult<AmsCollectResultVO> searchAmsCollectPageList(AmsCollectSearchVO searchVO) {
        PageResult<AmsCollect> dbPageResult = amsCollectMapper.selectPage(searchVO);
        List<AmsCollectResultVO> resultList = BeanUtil.copyToList(dbPageResult.getList(),AmsCollectResultVO.class);
        PageResult<AmsCollectResultVO> result = new PageResult<>();
        result.setTotal(dbPageResult.getTotal());
        result.setList(resultList);
        return result;
    }

    /**
     * 查询文章收藏详情
     */
    @Override
    public AmsCollectResultVO selectAmsCollectById(Long id) {
        AmsCollect dbInfo = amsCollectMapper.selectById(id);
        AmsCollectResultVO result = BeanUtil.copyProperties(dbInfo,AmsCollectResultVO.class);
        return result;
    }

    /**
     * 新增文章收藏
     */
    @Override
    public int saveAmsCollect(AmsCollectSaveVO saveVO) {
        long userId = SecurityUtils.getMemberUserId();
        if(userId == 0) {
            throw new ServiceException(HttpStatus.FORBIDDEN,"");
        }
        AmsCollect insertBean = BeanUtil.copyProperties(saveVO,AmsCollect.class);
        insertBean.setUserId(userId);
        insertBean.setCreateTime(LocalDateTime.now());
        LambdaUpdateWrapper<AmsArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("collect_volume = collect_volume + 1").eq(AmsArticle::getId,saveVO.getArticleId());
        amsArticleMapper.update(wrapper);
        return amsCollectMapper.insert(insertBean);
    }


    @Override
    public int cancel(AmsCollectSaveVO saveVO) {
        long userId = SecurityUtils.getMemberUserId();
        if(userId == 0) {
            throw new ServiceException(HttpStatus.FORBIDDEN,"");
        }
        int row = amsCollectMapper.delete(new LambdaUpdateWrapper<AmsCollect>().eq(AmsCollect::getArticleId, saveVO.getArticleId()).eq(AmsCollect::getUserId, userId));
        if(row > 0) {
            LambdaUpdateWrapper<AmsArticle> wrapper = new LambdaUpdateWrapper<>();
            wrapper.setSql("collect_volume = collect_volume - 1").eq(AmsArticle::getId,saveVO.getArticleId());
            amsArticleMapper.update(wrapper);
        }
        return row;

    }

    /**
     * 修改文章收藏
     */
    @Override
    public int updateAmsCollect(AmsCollectSaveVO saveVO) {
        AmsCollect updateBean = BeanUtil.copyProperties(saveVO,AmsCollect.class);
        return amsCollectMapper.updateById(updateBean);
    }

    /**
     * 批量删除文章收藏
     */
    @Override
    public int removeAmsCollectByIds(CommonParamIdVO idVO) {
        if(ArrayUtil.isNotEmpty(idVO.getIds())) {
            return amsCollectMapper.deleteBatchIds(Arrays.asList(idVO.getIds()));
        }else {
            return amsCollectMapper.deleteById(idVO.getId());
        }

    }
}
